============================================================================== F O R M U L A S 2 . 0 03/17/95 Here are a few interesting formulas which I have gleaned from some 27 years in computers. Some of these will be familiar to you, some may not. File FORMULAS.TXT (this file) is an ASCII file that should be readable on any screen or printer. File FORMULAS.DOC is a Word for Windows 2.0 document. Most word processors should be able to read this format. All of these formulas have been tested and work properly. The calendar functions have been tested for all dates from 01/01/0001 through 12/31/5000. Though these dates are meaningless, you can depend on the formulas for precision on real dates. If you are interested in other constants, or constants to much greater precision, you might want to try my BIGCALC program which emulates a Hewlett-Packard calculator on screen. BIGCALC has a four register stack and 10 memory registers. You can specify the precision from 3 to 1075 digits. The constants ( and e are provided to full precision. BIGCALC was used to derive the constants below. BIGCALC is available on several Forums on Compuserve, or send me $20 and I will provide you with a disk containing the latest version of BIGCALC (with C source code), a prime number program, and some other goodies. I would be happy to receive comments on this stuff, especially any new formulas or algorithms! Enjoy. Judson D. McClendon Sun Valley Systems 329 37th Court N.E. Birmingham, AL 35215 205-853-8440 Compuserve [74415,1003] ============================================================================== Some Important Constants: pi: 3.14159 26535 89793 23846 26433 83279 50288 41971 69399 37510 58209 74944 59230 78164 06286 20899 86280 34825 34211 70679 82 1/pi: 0.31830 98861 83790 67153 77675 26745 02872 40689 19291 48091 28974 95334 68811 77935 95268 45307 01802 27605 53250 61719 12 pi^2: 9.86960 44010 89358 61883 44909 99876 15113 53136 99407 24079 06264 13349 37622 00448 22419 20524 30017 73403 71855 22318 24 pi/180: 0.01745 32925 19943 29576 92369 07684 88612 71344 28718 88541 72545 60971 91440 17100 91146 03449 44368 22415 69634 50948 22 180/pi: 57.29577 95130 82320 87679 81548 14105 17033 24054 72466 56432 15491 60243 86120 28471 48321 55263 24409 68995 85111 09441 86 e: 2.71828 18284 59045 23536 02874 71352 66249 77572 47093 69995 95749 66967 62772 40766 30353 54759 45713 82178 52516 64274 27 1/e: 0.36787 94411 71442 32159 55237 70161 46086 74458 11131 03176 78345 07836 80169 74614 95744 89980 33571 47274 34591 96437 46 e^2: 7.38905 60989 30650 22723 04274 60575 00781 31803 15570 55184 73240 87127 82252 25737 96079 05776 33843 12485 07912 17947 73 Sqrt(2): 1.41421 35623 73095 04880 16887 24209 69807 85696 71875 37694 80731 76679 73799 07324 78462 10703 88503 87534 32764 15727 35 Sqrt(3): 1.73205 08075 68877 29352 74463 41505 87236 69428 05253 81038 06280 55806 97945 19330 16908 80003 70811 46186 75724 85756 75 Golden Ratio: 1.61803 39887 49894 84820 45868 34365 63811 77203 09179 80576 (Sqrt(5)+1)/2 28621 35448 62270 52604 62818 90244 97072 07204 18939 11374 84 ============================================================================== Newton's Method: Newton's Method for extracting positive integral roots of positive numbers is as follows: The rth root of the positive number n is obtained as a root of the function: f(a) = a^r - n by means of the iterated equation: aj = ai - (ai^r - n) / (r * ai^(r-1)) = ai * (1 - 1/r) + (n / (r * ai^(r-1))) For square root, the equation simplifies to aj = (ai + (n / ai)) / 2 Where: ai = previous approximation aj = next approximation r = root n = number To use Newton's Method you would first obtain a first approximation to the root. For square root, you can start by dividing the exponent of the number by 2 for the exponent of the first approximation, using 3 as the first digit. Then you would iterate the equation above until the desired accuracy is obtained. Comparing successing roots is a good way to determine that. Newton's method converges very rapidly if a reasonable first approximation is obtained. Integer Power Function: This method calculates y^x using the Square-Multiply algorithm. If you need to calculate some y to an integer power x > 0, this is a very efficient algorithm. shift x left 1 bit set rightmost bit of x to 1 (flag bit) shift x left until high order bit is 1 shift x left again set p = y while x <> (high order bit = 1, rest = 0) square p if high order bit of x = 1 multiply y times p shift x left 1 bit result in p is y^x Where: y = number x = positive integer power p = temporary variable which will contain the answer Zeller's Congruence: Zeller's Congruence is used to determine the day of the week of any date in the Gregorian calendar, which was instituted October 15, 1582. The formula, good for any date since October 15, 1582 is as follows: w = ( d + (m*2) + int((m+1)*3/5) + y + int(y/4) - int(y/100) + int(y/400) + 1 ) mod 7 Where: w = weekday (0=Sun, 1=Mon, 2=Tue, 3=Wed, 4=Thu, 5=Fri, 6=Sat) d = day of the month m = month of year (Jan & Feb = 13 & 14 of prev year) y = year (year - 1 if month is Jan or Feb) int = integer part (eg: int(3.5) = 3) mod = modulus or remainder part (eg: (10 mod 7) = 3) Example: Feb 12, 1809 d = 12, m = 14, y = 1808 (w = 0 Sun) Jul 4, 1776 d = 4, m = 7, y = 1776 (w = 4 Thu) Date Day Number: Date Day Number is used to determine the number of days between any two dates in the Gregorian calendar, which was instituted October 15, 1582. Date day number calculates a number which is one greater for each succeeding date. The formula, good for any date since October 15, 1582 is as follows: n = ( (y*365) + int(y/4) - int(y/100) + int(y/400) + int(m*306001/10000) + d ) Where: n = day number (Note that day number can be 6 digits or more) d = day of the month m = m + 13 (Jan & Feb) m + 1 (Mar - Dec) y = y - 1 (Jan & Feb) y (Mar - Dec) int = integer part (eg: int(3.5) = 3) Example: Feb 12, 1809 d = 12, m = 15, y = 1808 (n = 660,829) Jul 4, 1776 d = 4, m = 8, y = 1776 (n = 648,919) (days between = 11,910) Easter Computation: The following algorithm comes from volume one of 'The Art of Computer Programming', by Donald Knuth, Second Edition, pages 155-156. Let y be the year for which the date of Easter is desired. g = (y mod 19) + 1 (g is the so-called "golden number" of the year in the 19-year Metonic cycle.) c = int(y / 100) + 1 (c is the century number.) x = int(3 * c / 4) - 12 (x is the number of years, such as 1900, in which leap year was dropped to keep in step with the sun.) z = int((8 * c + 5) / 25) - 5 (z is a special correction designed to synchronize Easter with the moon's orbit.) d = int(5 * y / 4) - x - 10 (d is a factor to adjust the date to the following Sunday.) e = abs(11 * g + 20 + z - x) mod 30 if (e = 25 and g > 11) (e is the so-called "epact" which or specifies when a full moon occurs.) (e = 24) e = e + 1 (Easter is supposedly the "first Sunday following n = 44 - e the first full moon which occurs on or after March if (n < 21) 21." Actually perturbations in the moon's orbit n = n + 30 do not make this strictly true, but we are concerned here with the "calendar moon" rather than the actual moon. The 'n'th of March is a calendar full moon.) n = n + 7 - ((d + n) mod 7) (Advance to following Sunday.) if (n > 31) (If n > 31 then Easter falls in the month = 4 month of April instead of March.) day = n - 31 else month = 3 day = n Where: int = integer part (eg: int(3.5) = 3) mod = modulus or remainder part (eg: (10 mod 7) = 3) abs = absolute value (eg: abs(3) = 3, abs(-3) = 3) Note that this algorithm is only valid for the Gregorian Calendar. In other words, years 1583 forward. All variables are integers. If you calculate Easter for years far into the future some of the variables may overflow 16 bit integers. Fast YYMMDD <-> MMDDYY Conversion in COBOL: If you need a quick-and-dirty conversion of 6-digit dates between MMDDYY and YYMMDD form in COBOL, this might be useful to you. COMPUTE DATE-YYMMDD = DATE-MMDDYY * 10000.01 COMPUTE DATE-MMDDYY = DATE-YYMMDD * 100.0001 This works fine as long as you DO NOT use ROUNDED, and the result is calculated into a 6-digit field.